package com.fbty.mt.action.user;

import java.util.ArrayList;
import java.util.List;

import com.fbty.base.action.BaseAction;
import com.fbty.mt.domain.IpItem;
import com.fbty.mt.domain.Item;
import com.fbty.mt.domain.OnlyIpInfo;
import com.fbty.mt.domain.Price;
import com.fbty.mt.domain.User;
import com.fbty.mt.domain.UserIpInfo;
import com.fbty.mt.service.IpItemService;
import com.fbty.mt.service.ItemService;
import com.fbty.mt.service.PriceService;
import com.fbty.mt.service.RecordService;
import com.fbty.mt.service.UserIpInfoService;
import com.fbty.mt.service.UserService;
import com.fbty.mt.utils.OrderUtil;

public class BuyAction extends BaseAction {
	
	private int state;
	
	
	private int priceId;
	
	private ItemService itemService;
	
	private PriceService priceService;
	
	private RecordService recordService;
	
	private UserIpInfoService userIpInfoService;
	
	private UserService userService;
	
	private IpItemService ipItemService;
	
	private int itemId;
	
	private Item item;
	
	private int ipid;
	
	
	private List<Float> days;
	
	private Double total1;
	
	private List<String> ips;
	
	private String orderNo;
	//private int days;
	
	private String payKey;//支付宝帐号
	private double price;//支付价格
	private String remark;//描述
	
	public String buyOnlyIp(){
		User u = (User)sessionMap.get("user");
		
		
		if(u!=null){
			//生成订单
			orderNo = OrderUtil.getOrderId(u.getId());
			
			item = new Item();
			item.setBuyTime(System.currentTimeMillis());
			item.setItemNo(orderNo);
			item.setPrice(total1);
			item.setOnly(1);
			item.setUid(u.getId());
			
			int id = itemService.saveItem(item);
			//保存订单和IP的关系表
			List<IpItem> ipitems = new ArrayList<IpItem>();
			for(int i=0;i<ips.size();i++){
				IpItem item = new IpItem();
				item.setItemNo(orderNo);
				item.setOnlyIp(ips.get(i));
				item.setDays(days.get(i));
				ipitems.add(item);
			}
			//保存订单的同时，将独享IP的state置为2，未付款状态
			ipItemService.saveBach(ipitems);
			
			//清空购物车
			sessionMap.remove("ipItems");
			
//			try {
//				int s = userIpInfoService.queryIpState(ipid);
//				if(s==0){
//					OnlyIpInfo ip = userIpInfoService.findIpById(ipid);
//					double price = ip.getPrice()*days;
//					itemId = itemService.saveItem(u.getId(), ipid,price,days,true);
//					item = itemService.finItemById(itemId);
//				}else{
//					return "show";
//				}
//			} catch (Exception e) {
//				e.printStackTrace();
//			}
			//独享套餐 -----弃用
			/*int s = userIpInfoService.queryIpState(ipid);
			if(s==0){
				int num = recordService.getOnlyNum(u.getId());
				if(num>0){
					int days = recordService.getUseTime(u.getId());
					UserIpInfo info = new UserIpInfo();
					info.setEndTime(System.currentTimeMillis()+(days*24*60*60*1000));
					info.setFlag(false);
					info.setIpid(ipid);
					info.setIptype(1);
					info.setUid(u.getId());
					userIpInfoService.saveMyOnlyIp(info);
					state = 1;
				}else{
					state = 2;
				}
			}else{
				state = 3;
			}*/
		}
		return "order";
	}
	
	public String buyIp(){
		User u = (User)sessionMap.get("user");
		
		if(u!=null){
			int monthNum = userService.updateMonthNum(u.getId());
			if(monthNum>0){
				if(userIpInfoService.repeat(ipid, u.getId())){
					UserIpInfo info = new UserIpInfo();
					info.setEndTime(System.currentTimeMillis());
					info.setFlag(false);
					info.setIpid(ipid);
					info.setUid(u.getId());
					info.setAutoChange(false);
					info.setNum(0);
					userIpInfoService.saveMyIpInfo(info,monthNum,true);
					sessionMap.put("monthNum", monthNum-1);
					state=3;
				}else{
					state = 2;
				}
			}else{
				int num = userService.getNum(u.getId());
				if(num>0){
					if(userIpInfoService.repeat(ipid, u.getId())){
						UserIpInfo info = new UserIpInfo();
						info.setEndTime(System.currentTimeMillis());
						info.setFlag(false);
						info.setIpid(ipid);
						info.setUid(u.getId());
						info.setAutoChange(false);
						info.setNum(0);
						userIpInfoService.saveMyIpInfo(info,num,false);
						sessionMap.put("num", u.getNum()-1);
						state=3;
					}else{
						state = 2;
					}
				}else{
					state = 1;
				}
			}
		}
		return "buyip";
		
	}
	
	public String buyPrice(){
		User u = (User)sessionMap.get("user");
		if(u==null){
			return "login";
		}
		Price price = priceService.findPriceById(priceId);
		itemId = itemService.saveItem(u.getId(), price.getId(),price.getPrice(),0,false);
		item = itemService.finItemById(itemId);
		return "buyprice";
	}

	/**
	 * 
	 * Description:刷新价格 <br>
	 * @return
	 */
	public String refresh(){
		item = itemService.finItemById(itemId);
		return "buyprice";
	}
	
	/**
	 * 取消订单
	 * @return
	 */
	public String delete(){
		itemService.doChangeItem(itemId);
		return "price";
	}
	
	public String sureBuy(){
		User u = (User)sessionMap.get("user");
		if(u==null){
			return "login";
		}
		item = itemService.finItemById(itemId);
		payKey=item.getItemNo();
		price = item.getPrice();
		if(item.getOnly()==1){
			remark ="订单号"+payKey+","+u.getUsername()+"购买独享IP价格为"+price+"!";
		}else{
			remark ="订单号"+payKey+","+u.getUsername()+"购买套餐价格为"+price+"!";
		}
		return "sure";
	}
	
	public int getState() {
		return state;
	}

	public void setState(int state) {
		this.state = state;
	}

	public void setPriceId(int priceId) {
		this.priceId = priceId;
	}

	public void setItemService(ItemService itemService) {
		this.itemService = itemService;
	}


	public void setPriceService(PriceService priceService) {
		this.priceService = priceService;
	}

	public int getItemId() {
		return itemId;
	}

	public void setItemId(int itemId) {
		this.itemId = itemId;
	}

	public Item getItem() {
		return item;
	}

	public int getIpid() {
		return ipid;
	}

	public void setIpid(int ipid) {
		this.ipid = ipid;
	}

	public void setRecordService(RecordService recordService) {
		this.recordService = recordService;
	}

	public void setUserIpInfoService(UserIpInfoService userIpInfoService) {
		this.userIpInfoService = userIpInfoService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	
	public void setDays(List<Float> days) {
		this.days = days;
	}
	
	public List<Float> getDays() {
		return days;
	}
	
	public void setIps(List<String> ips) {
		this.ips = ips;
	}
	
	public List<String> getIps() {
		return ips;
	}
	
	

	
	public String getPayKey() {
		return payKey;
	}

	public double getPrice() {
		return price;
	}

	public void setPrice(double price) {
		this.price = price;
	}

	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}
	
	
	public void setTotal1(Double total1) {
		this.total1 = total1;
	}
	
	public Double getTotal1() {
		return total1;
	}
	
	public void setIpItemService(IpItemService ipItemService) {
		this.ipItemService = ipItemService;
	}
	
}
